package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zmht.beike.biz.CaimtostudentBiz;
import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TCaimtostudent;
import com.zmht.beike.po.TCareacontent;
import com.zmht.specedu.vo.Teacher;

public class MuBiaoAction extends BaseAction {
	
	public ActionForward initAddMuBiao(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		//获得集体目标对应的学生
		List list = this.getCareaContentBiz().getCareaContentDAO().findBySql("select s.StudentId,s.SName,s.ClassID from classteacher ct,student s where s.ClassID=ct.ClassID and ct.TeacherID = "+teacher.getTeacherId());
		//System.out.println(teacher.getTeacherId());
		request.setAttribute("teacherName", teacher.getTname());
		request.setAttribute("students", list);
		return mapping.findForward("addMubiao");
	}
	/**
	 *初始化目标添加页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initMubiao(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String type  = request.getParameter("type");
		Integer index = 1;
		Integer size = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			size = new Integer(request.getParameter("pageSize"));
		}
		List list = this.getCareaContentBiz().findAreaContent(request.getSession().getId(), new Integer(type),(index-1)*size,size);		
		int total = this.getCareaContentBiz().findAreaContentTotal(request.getSession().getId(), new Integer(type));
		
		
		
		
		request.setAttribute("currPage", index); //��ǰҳ��
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(size)));//��ҳ��
		request.setAttribute("pageSize", size);// ÿҳ��С
		
		request.setAttribute("areaType", type);
		request.setAttribute("type", type);
		request.setAttribute("list", list);
		return mapping.findForward("lingyu");
	
	}
	/**
	 * 编辑目标时调用 此时查询当前目标下所有的内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editMubiaoContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		String type  = request.getParameter("type");
		Integer targetID = new Integer(request.getParameter("targetId"));
		
		
		Integer index = 1;
		Integer pageSize = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		if(null!=request.getParameter("pageSize")){
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		
		List list =this.getCareaContentBiz().findAreaContentEdit(targetID, teacher.getTeacherId(), new Integer(type), (index-1)*pageSize, pageSize);//this.getCareaContentBiz().findAreaContent(request.getSession().getId(), new Integer(type),(index-1)*size,size);		
		int total = this.getCareaContentBiz().findAreaContentEditTotal(targetID, teacher.getTeacherId(), new Integer(type));//findAreaContentTotal(request.getSession().getId(), new Integer(type));
		
		
		
		
		request.setAttribute("currPage", index); //��ǰҳ��
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(pageSize)));//��ҳ��
		request.setAttribute("pageSize", pageSize);// ÿҳ��С
		
		request.setAttribute("areaType", type);
		request.setAttribute("type", type);
		request.setAttribute("list", list);
		
		//标记为 编辑页面跳转
		request.setAttribute("edit", "1");
		request.setAttribute("targetId", targetID);
		return mapping.findForward("lingyu");
	
	}
	/**
	 * 添加集体目标内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addLingYuContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String type  = request.getParameter("type");
		String contact =request.getParameter("contact");
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		TCareacontent content = new TCareacontent();
		content.setAreaContent(contact);
		content.setAreaType(new Integer(type));
		content.setSessionID(request.getSession().getId());
		content.setTeacherId(teacher.getTeacherId());
		if("1".equals(request.getParameter("edit"))){
			content.setCaimId(new Integer(request.getParameter("targetId")));
		}
		
		//保存记录
		this.getCareaContentBiz().saveLinyuContent(content);
	
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			return new ActionForward("/beike/jitimubiao.do?method=editMubiaoContent&type="+type+"&targetId="+targetId);
		}
		
		return new ActionForward("/beike/jitimubiao.do?method=initMubiao&type="+type);
	}
	/**
	 * 删除领域目标内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delLingYuContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String type= request.getParameter("type");
		String tid = request.getParameter("tid");
		
		
		java.io.Serializable obj = this.getCareaContentBiz().findByID(new Integer(tid));
		if(obj!=null){
			this.getCareaContentBiz().delTCareacontent(obj);
		}
		System.out.println("---------------del----------------");
/*		//开始查询
		Integer index = 1;
		Integer size = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		
		List list = this.getCareaContentBiz().findAreaContent(request.getSession().getId(), new Integer(type),(index-1)*size,size);		
		int total = this.getCareaContentBiz().findAreaContentTotal(request.getSession().getId(), new Integer(type));
		
		
		request.setAttribute("currPage", index); //��ǰҳ��
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(size)));//��ҳ��
		request.setAttribute("pageSize", size);// ÿҳ��С
		
		request.setAttribute("areaType", type);
		request.setAttribute("list", list);*/
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			return new ActionForward("/beike/jitimubiao.do?method=editMubiaoContent&type="+type+"&targetId="+targetId);
		}
		return new ActionForward("/beike/jitimubiao.do?method=initMubiao&type="+type);
		//return mapping.findForward("lingyu");
		

	}
	/**
	 * 编辑领域目标内容�༭
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editLingYuContact(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		String type= request.getParameter("type");
		String tid = request.getParameter("tid");
		
		String ct = request.getParameter("contact");
		
		TCareacontent content = this.getCareaContentBiz().findByID(new Integer(tid));
		if(content!=null){
			content.setAreaContent(ct);
			this.getCareaContentBiz().upTCareacontent(content);
		}
		System.out.println("update");
		//开始查询
		/*Integer index = 1;
		Integer size = 5;
		if(null!=request.getParameter("currPage")){
			index = new Integer(request.getParameter("currPage"));
		}
		
		List list = this.getCareaContentBiz().findAreaContent(request.getSession().getId(), new Integer(type),(index-1)*size,size);
		
		int total = this.getCareaContentBiz().findAreaContentTotal(request.getSession().getId(), new Integer(type));
		request.setAttribute("currPage", index); //��ǰҳ��
		request.setAttribute("pageAmount", (int)Math.ceil(total/new Double(size)));//��ҳ��
		request.setAttribute("pageSize", size);// ÿҳ��С
		
		request.setAttribute("areaType", type);
		request.setAttribute("list", list);
		*/
		if("1".equals(request.getParameter("edit"))){
			String targetId = request.getParameter("targetId");
			return new ActionForward("/beike/jitimubiao.do?method=editMubiaoContent&type="+type+"&targetId="+targetId);
		}
		return new ActionForward("/beike/jitimubiao.do?method=initMubiao&type="+type);
		
	}
	/**
	 * 初始化编辑
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initMubiaoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return null;
	
	}
	public static void main(String [] args){
		ApplicationContext cxt;
		cxt = new ClassPathXmlApplicationContext(new String[]{"spring/spring.xml","spring/spring_dao.xml","spring/spring_beike.xml"});
		CaimtostudentBiz biz = (CaimtostudentBiz)cxt.getBean("caimtostudentBizTag");
		//List list = biz.getCaimtostudentDAO().findBySql("select * from t_caimtostudent");
		TCaimtostudent bean = new TCaimtostudent();
		bean.setCaimId(2);
		bean.setTeacherId(4);
		bean.setStudentId(2);
		bean.setCaimId(1);
		
		System.out.println(biz.getCaimtostudentDAO().saveObj(bean));
	}
}
